package carltechnology.com.mahjonggame.utils.dao.impl;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import carltechnology.com.mahjonggame.mode.IData;
import carltechnology.com.mahjonggame.mode.impl.MahjongGameDetailMode;
import carltechnology.com.mahjonggame.mode.impl.MahjongGameMode;
import carltechnology.com.mahjonggame.utils.dao.IMahjongDao;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;

/**
 * Created by WZTCM on 2016/6/1.
 */
public class MahjongGameDao implements IMahjongDao {
    private static String TAG = MahjongGameDao.class.getSimpleName();
    private MyDataBaseHelper myDataBaseHelper ;
    private Context mContext;
    public MahjongGameDao(Context mContext) {
        this.mContext = mContext;
        myDataBaseHelper =
                new MyDataBaseHelper(mContext, MyDataBaseHelper.DATABASE_INFO.DB_NAME.toString(),null,1);
        myDataBaseHelper.getWritableDatabase();
    }

    @Override
    public void getMahjongGameAllInfo(Subscriber<? super IData> subscriber) {
       // Log.d(TAG,"begin info");
       // System.out.println(Thread.currentThread());
        SQLiteDatabase db = myDataBaseHelper.getWritableDatabase();
        // 查询Game表的内容
        Cursor cursor = null;
        //System.out.println(Thread.currentThread());
        try{
            cursor = db.query("game",null,null,null,null,null,"date desc");
            if (cursor.moveToFirst()){
                do {
                    MahjongGameMode mahjongGameMode = MahjongGameMode._of(
                            cursor.getInt(cursor.getColumnIndex("id")),
                            cursor.getString(cursor.getColumnIndex("name")),
                            cursor.getString(cursor.getColumnIndex("date"))
                    );
                    //System.out.println(mahjongGameMode.toString());
                    subscriber.onNext(mahjongGameMode);
                }while(cursor.moveToNext());
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (cursor!=null)
                cursor.close();

        }
    }

    @Override
    public Subscription storeMahjongGameInfo(Observable<ContentValues> observable) {
        return observable.subscribe(getStoreMahjongGameSubs());
    }

    @Override
    public void getMahjongDetailGameAllInfo(Subscriber<? super IData> subscriber,String id) {
        SQLiteDatabase db = myDataBaseHelper.getWritableDatabase();
        // 查询Game表的内容
        Cursor cursor = null;
        //System.out.println(Thread.currentThread());
        try{
            cursor = db.query("gamedetail",null,"id="+id,null,null,null,"date desc");
            if (cursor.moveToFirst()){
                do {
                    int [] price = new int [4];
                    int cost;
                    int perunit;
                    String date;
                    int perId;
                    price[0] = cursor.getInt(cursor.getColumnIndex("income1"));
                    price[1] = cursor.getInt(cursor.getColumnIndex("income2"));
                    price[2] = cursor.getInt(cursor.getColumnIndex("income3"));
                    price[3] = cursor.getInt(cursor.getColumnIndex("income4"));
                    cost = cursor.getInt(cursor.getColumnIndex("cost"));
                    perunit = cursor.getInt(cursor.getColumnIndex("perunit"));
                    date = cursor.getString(cursor.getColumnIndex("date"));
                    perId = cursor.getInt(cursor.getColumnIndex("perid"));
                    MahjongGameDetailMode mode = MahjongGameDetailMode._of(perId,price,cost,perunit,date);
                    subscriber.onNext(mode);
                }while(cursor.moveToNext());
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (cursor!=null)
                cursor.close();

        }
    }

    public Subscriber<ContentValues> getStoreMahjongGameSubs(){
        return new Subscriber<ContentValues>() {
            @Override
            public void onCompleted() {
                Log.d("store","finished");
            }

            @Override
            public void onError(Throwable e) {
                Log.d("store","error"+e.toString());
            }

            @Override
            public void onNext(ContentValues contentValues) {
                SQLiteDatabase db = myDataBaseHelper.getWritableDatabase();
                db.insert(MyDataBaseHelper.DATABASE_INFO.MAHJONGGAME_DATABASE_NAME.toString()
                        , null
                        ,contentValues);
            }
        };
    }


}
